import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)

export default new Vuex.Store({
        state: {
            loading: false, //加载
            mainMenus: [], // 系统主菜单
            clickItemObj: {},
            selectCrumbs: {},
            allCrumbs: [],
            phoneNature: [],
            natureArr: {},
            bigScreenNature: {
                natureArr: []
            }, // bi报表大屏模式
            dialogEditors: [{}], // 编辑器弹窗（存个数）
            biDSUrls: {},
            snapshotData: [], // 编辑器快照数据
            snapshotIndex: -1, // 快照索引
            menuTop: 0,
            menuLeft: 0,
            menuShow: false,
            curComponent: null, // 当前激活的元素
            curComponentZIndex: null,
            biNatureArrIds: {}, //bi报表的控件id字典
            selectData: {},
            biSelectTable: {}, // 选中单元格时，保存Table
            screenData: [],
            dataSource: [], //数据源
            // 弹窗关闭事件
            closeDialog() {},
            // 面包屑页签关闭标签对象
            closePageObj: {},
            // 计划任务开关事件
            taskSwitch() {},
            // 计划任务开关状态
            moduleTaskSwitch: false,
            // 菜单开关状态
            menuSmallType: false,
            // bi报表手机版数据
            biMobileObj: {},
            // bi报表手机版是否触发数据改变
            biMobileIsDataChange: false,
            // bi报表是否新增的标识
            isBIAdd: {},
            mobileMouseRightTop: 0,
            mobileMouseRightLeft: 0,
            mobileMouseRightShow: false
        },
        mutations: {
            setLoading(status, data) {
                status.loading = data
            },
            setMainMenus(status, data) {
                status.mainMenus = data
            },
            // fd表单界面设计器开始
            resetCrumbs(status, data) {
                status.allCrumbs = []
                status.selectCrumbs = {}
            },
            clickItem(status, data) {
                status.clickItemObj = data
            },
            addAllCrumbs(status, data) { //添加
                allCrumbsPush(status.allCrumbs, data)
            },
            delAllCrumbs(status, data) { //删除
                allCrumbsSplice(status, status.allCrumbs, data)
            },
            setSelectCrumbs(status, data) { // fg自定义报表组件区域
                let id = typeof data == 'string' ? data : data.id
                status.selectCrumbs = status.allCrumbs.find(all => all.id == id)
            },

            // fd表单界面设计器结束
            setDataSource(status, value) { // 数据源
                status.dataSource = value
            },
            setPhoneNature(status, value) { // BI自定义报表组件区域
                status.phoneNature = value
            },
            setBIAdd(status, data) { // BI报表是否新增
                if (status.dialogEditors.length == 0) {
                    return
                }
                if (typeof data == 'boolean') {
                    const i = 'd' + (status.dialogEditors.length - 1)
                    status.isBIAdd[i] = data
                } else {
                    status.isBIAdd = data
                }
            },
            // 保存预览时的URL
            setBiDSUrls(status, data) {
                if (status.dialogEditors.length == 0) {
                    return
                }
                if (Array.isArray(data)) {
                    const i = 'd' + (status.dialogEditors.length - 1)
                    status.biDSUrls[i] = data
                } else {
                    status.biDSUrls = data
                }
            },
            setDialogEditors(status, data) { // BI报表是否新增
                status.dialogEditors = data
            },
            setNatureArr(status, data) { // BI自定义报表组件区域
                //改成有index的字典对象,由此应对多设计区的页面
                if (status.dialogEditors.length == 0) {
                    return
                }
                if (Array.isArray(data)) {
                    const i = 'd' + (status.dialogEditors.length - 1)
                    status.natureArr[i] = []
                    status.natureArr[i] = data
                    status.natureArr = {...status.natureArr }
                } else {
                    status.natureArr = data
                }
            },
            setBISelectTable(status, data) {
                status.biSelectTable = data
            },
            setBigScreenNature(status, data) { // BI自定义报表组件区域-大屏模式
                status.bigScreenNature = data
            },
            recordSnapshot(state) {
                // 添加新的快照
                // state.snapshotData[++state.snapshotIndex] = utils.deepCopy(state.bigScreenNature.natureArr)
                // // 在 undo 过程中，添加新的快照时，要将它后面的快照清理掉
                // if (state.snapshotIndex < state.snapshotData.length - 1) {
                // state.snapshotData = state.snapshotData.slice(0, state.snapshotIndex + 1)
                // }
            },
            setCurComponent(state, { component, zIndex }) {
                state.curComponent = component
                state.curComponentZIndex = zIndex
            },
            setBigArr(state, { index, selectData }) {
                state.bigScreenNature.natureArr[index] = selectData
            },
            setShapeStyle(state, { top, left, width, height }) {
                if (top) state.curComponent.style.top = top
                if (left) state.curComponent.style.left = left
                if (width) state.curComponent.style.width = width
                if (height) state.curComponent.style.height = height
                state.bigScreenNature.natureArr[state.curComponentZIndex] = state.curComponent
                state.bigScreenNature = {...state.bigScreenNature }
            },
            setShapePosStyle(state, { key, value }) {
                state.curComponent.style[key] = value
            },
            setBiNatureArrIds(status, data) { // BI自定义报表ID集合
                status.biNatureArrIds = data
            },
            showContexeMenu(state, { top, left }) {
                state.menuShow = true
                state.menuTop = top
                state.menuLeft = left
            },

            hideContexeMenu(state) {
                state.menuShow = false
            },

            deleteComponent(state) {
                state.bigScreenNature.natureArr.splice(state.curComponentZIndex, 1)
                state.bigScreenNature = {...state.bigScreenNature }
            },
            copyComponent(state, newId) {
                let newComponent = {...state.curComponent }
                newComponent.id = newId
                newComponent.style.left = newComponent.style.left + newComponent.style.width
                state.bigScreenNature.natureArr.push(newComponent)
                state.bigScreenNature = {...state.bigScreenNature }
            },
            upComponent() {
                // 上移图层 zIndex，表示元素在数组中越往后
                let { bigScreenNature, curComponentZIndex } = state
                if (curComponentZIndex < bigScreenNature.natureArr.length - 1) {
                    swap(bigScreenNature.natureArr, curComponentZIndex, curComponentZIndex + 1)
                    state.bigScreenNature = {...state.bigScreenNature }
                } else {
                    console.log('已经到顶了', 'error')
                }
            },

            downComponent(state) {
                // 下移图层 zIndex，表示元素在数组中越往前
                let { bigScreenNature, curComponentZIndex } = state
                if (curComponentZIndex > 0) {
                    swap(bigScreenNature.natureArr, curComponentZIndex, curComponentZIndex - 1)
                    state.bigScreenNature = {...state.bigScreenNature }
                } else {
                    console.log('已经到底了', 'error')
                }
            },

            topComponent(state) {
                // 置顶
                let { bigScreenNature, curComponentZIndex } = state
                if (curComponentZIndex < bigScreenNature.natureArr.length - 1) {
                    swap(bigScreenNature.natureArr, curComponentZIndex, bigScreenNature.natureArr.length - 1)
                    state.bigScreenNature = {...state.bigScreenNature }
                } else {
                    console.log('已经到顶了', 'error')
                }
            },

            bottomComponent(state) {
                // 置底
                let { bigScreenNature, curComponentZIndex } = state
                if (curComponentZIndex > 0) {
                    swap(bigScreenNature.natureArr, curComponentZIndex, 0)
                    state.bigScreenNature = {...state.bigScreenNature }
                } else {
                    console.log('已经到底了', 'error')
                }
            },
            setSelectData(status, data) { // 更新选择项
                status.selectData = data
            },
            saveArr(status, { data, biModel }) { // 更新选择项与组件区域关联
                if (biModel == 'mobile') {
                    handlerItem(data, status.phoneNature)
                } else {
                    handlerItem(data, status.natureArr['d' + (status.dialogEditors.length - 1)])
                }
            },
            setScreenData(status, data) { // 更新筛选框
                status.screenData = data
            },
            // 弹窗关闭设置
            setDialog(state, v) {
                if (typeof v === 'function') {
                    state.closeDialog = v
                }
            },
            // 面包屑页签关闭标签对象值设置
            setClosePageObj(state, v) {
                state.closePageObj = v
            },
            // 设置计划任务开关
            setTaskSwitch(state, v) {
                if (typeof v === 'function') {
                    state.taskSwitch = v
                }
            },
            // 设置计划任务开关状态设置
            setModuleTaskSwitch(state, v) {
                state.moduleTaskSwitch = v
            },
            // 菜单开关状态设置
            setMenuSmallType(state, v) {
                state.menuSmallType = v
            },
            // bi报表手机版父节点id
            setBiMobileObj(state, v) {
                state.biMobileObj = v
            },
            // bi报表手机版是否触发数据改变
            setBiMobileIsDataChange(state, v) {
                state.biMobileIsDataChange = v
            },
            // 手机右键
            setMobileMouseRight(state, { left, top, show }) {
                state.mobileMouseRightTop = top
                state.mobileMouseRightLeft = left
                state.mobileMouseRightShow = show
            }
        },
        actions: {},
        modules: {},
        getters: {
            finallCrumbs: state => {
                if (state.allCrumbs.length == 0) {
                    return state.allCrumbs
                }
                for (let res of state.allCrumbs) {
                    if (res.id == state.selectCrumbs.id) {
                        let data = JSON.parse(res.other)
                        data.natureArr = state.natureArr
                        res.other = JSON.stringify(data)
                        return state.allCrumbs
                    }
                }
                return state.allCrumbs
            }
        }
    })
    // 用于表单
export function allCrumbsPush(allCrumbs, data) {
    if (allCrumbs.length > 0) {
        let index = allCrumbs.findIndex(all => all.id == data.id)
        if (index == '-1') {
            allCrumbs.push(data)
        }
    } else {
        allCrumbs.push(data)
    }
}
export function allCrumbsSplice(status, allCrumbs, data) {
    let id = typeof data == 'string' ? data : data.id
    if (allCrumbs.length > 0) {
        let index = allCrumbs.findIndex(all => all.id == id)
        if (index != '-1') {
            let delData = allCrumbs.splice(index, 1)
            if (delData[0].id == status.selectCrumbs.id) { //如果删除与选中的是同一个 ，默认选中最后一个，如果没有面包屑，把选中的清空
                if (status.allCrumbs.length > 0) {
                    status.selectCrumbs = status.allCrumbs[index - 1]
                    status.natureArr = JSON.parse(status.selectCrumbs.other).natureArr
                } else {
                    status.selectCrumbs = {}
                    status.natureArr = {}
                }
            }
        }
    }
}
export function swap(arr, i, j) {
    const temp = arr[i]
    arr[i] = arr[j]
    arr[j] = temp
}
// 用于BI自定义报表数据
export function handlerItem(data, arr) {
    if (!setSelectedItem(data, arr)) {
        for (let natu of arr) {
            if (natu.type === 'tab') {
                for (let tab of natu.tabs) {
                    handlerItem(data, tab.natureArr)
                }
            } else if (natu.type === 'table') {
                handlerItem(data, natu.cols)
            } else if (natu.type === 'panel') {
                handlerItem(data, natu.natureArr)
            } else if (natu.type === 'element' && natu.state == '1') {
                handlerItem(data, natu.dataSourceData)
            } else if (natu.type === 'col' && natu.children && natu.children.length) {
                handlerItem(data, natu.children)
            }
        }
    }
}
// 用于BI自定义报表数据-找item
export function setSelectedItem(data, arr) {
    let result = false
    if (arr) {
        for (let i = 0; i < arr.length; i++) {
            if (arr[i].id === data.id) {
                arr[i] = data
                result = true
                break
            }
        }
    }
    return result
}